package com.mbbb.yygh.orders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mbbb.yygh.common.exception.YyghException;
import com.mbbb.yygh.enums.OrderStatusEnum;
import com.mbbb.yygh.enums.PaymentStatusEnum;
import com.mbbb.yygh.model.order.OrderInfo;
import com.mbbb.yygh.model.order.PaymentInfo;
import com.mbbb.yygh.orders.mapper.PaymentInfoMapper;
import com.mbbb.yygh.orders.service.OrderInfoService;
import com.mbbb.yygh.orders.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author mbbb
 * @since 2021-12-22
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderInfoService orderInfoService;

    //根据订单号获取支付记录
    @Override
    public PaymentInfo getPaymentInfo(Long orderId) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }

    //支付成功之后，更新支付状态
    @Override
    public void paySuccess(String outTradeNo, Integer paymentType, Map<String, String> paramMap) {
        //1.更新订单表中订单的状态 ：已经支付
        //根据outTradeNo查询
        QueryWrapper<OrderInfo> orderWrapper = new QueryWrapper();
        orderWrapper.eq("out_trade_no",outTradeNo);
        OrderInfo orderInfo = orderInfoService.getOne(orderWrapper);
        if (orderInfo == null)
            throw new YyghException(20001,"订单不存在");
        if (orderInfo.getOrderStatus() == OrderStatusEnum.PAID.getStatus())
            throw new YyghException(20001,"订单已支付");
        //设置状态已经支付
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        //更新订单表的状态
        orderInfoService.updateById(orderInfo);

        //2.更新支付记录表状态 ：已经支付
        QueryWrapper<PaymentInfo> paymentWrapper = new QueryWrapper<>();
        paymentWrapper.eq("out_trade_no",outTradeNo);
        PaymentInfo paymentInfo = baseMapper.selectOne(paymentWrapper);
        if (paymentInfo == null)
            throw new YyghException(20001,"订单不存在");
        if (paymentInfo.getPaymentStatus() == PaymentStatusEnum.PAID.getStatus())
            throw new YyghException(20001,"订单已支付");
        //设置状态已经支付
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setTradeNo(paramMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date()); //回调时间
        paymentInfo.setCallbackContent(paramMap.toString()); //回调信息

        baseMapper.updateById(paymentInfo);
    }

    //添加交易记录
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //判断支付表中是否存在相同的支付记录
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderInfo);
        wrapper.eq("payment_type",paymentType);
        Integer count = baseMapper.selectCount(wrapper);

        //存在相同的支付记录
        if (count > 0)
            return;

        //不存在相同的支付记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date()); //订单创建时间
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo()); //对外业务编号
        paymentInfo.setOrderId(orderInfo.getId()); //订单编号
        paymentInfo.setPaymentType(paymentType); //支付类型（微信 支付宝）
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus()); //支付状态
        paymentInfo.setTotalAmount(orderInfo.getAmount()); //支付金额
        //交易内容
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);

        //调用方法
        baseMapper.insert(paymentInfo);
    }
}
